// 获取书签数据并渲染到网页上
fetch('https://https://shaoclouds.gitee.io/storage-room/bookmarks.json')
  .then(response => response.json())
  .then(data => renderBookmarks(data));

function renderBookmarks(bookmarkData) {
    const bookmarksContainer = document.getElementById('bookmarks');
  
    bookmarkData.folders.forEach(folder => {
      const folderElement = document.createElement('div');
      folderElement.className = 'folder';
      folderElement.textContent = folder.name;
  
      const bookmarkList = document.createElement('ul');
      bookmarkList.className = 'bookmark-list';
  
      if (folder.bookmarks.length === 0) { // 如果文件夹为空，则显示提示信息
        const emptyMessageItem = document.createElement('li');
        emptyMessageItem.textContent = '该文件夹没有书签。';
        bookmarkList.appendChild(emptyMessageItem);
      } else { // 否则，添加每个书签到列表中
        folder.bookmarks.forEach(bookmark => {
          const bookmarkItem = document.createElement('li');
          if (bookmark.url === "#") { // 处理无效 URL 的情况（例如 "#"）
            bookmarkItem.textContent = bookmark.title;
          } else { // 创建具有链接的元素
            const linkElement = document.createElement('a');
            linkElement.href=bookmark.url; // 修改链接为书签 URL
            linkElement.target = '_blank';
            linkElement.textContent = bookmark.title;
            bookmarkItem.appendChild(linkElement);
          }
  
          bookmarkList.appendChild(bookmarkItem);
        });
      }
  
      folderElement.addEventListener("click", () => {
        folderElement.classList.toggle("open");
     });
  
     bookmarksContainer.appendChild(folderElement);
     bookmarksContainer.appendChild(bookmarkList);
   });
}
